package com.ruoyi.system.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.system.domain.FxyFinancialSubject;
import com.ruoyi.system.mapper.FxyFinancialSubjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.FxyFinancialAccountSetsMapper;
import com.ruoyi.system.domain.FxyFinancialAccountSets;
import com.ruoyi.system.service.IFxyFinancialAccountSetsService;

/**
 * 账套管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-15
 */
@Service
public class FxyFinancialAccountSetsServiceImpl implements IFxyFinancialAccountSetsService 
{
    @Autowired
    private FxyFinancialAccountSetsMapper fxyFinancialAccountSetsMapper;

    @Autowired
    private FxyFinancialSubjectMapper fxyFinancialSubjectMapper;

    /**
     * 查询账套管理
     * 
     * @param id 账套管理主键
     * @return 账套管理
     */
    @Override
    public FxyFinancialAccountSets selectFxyFinancialAccountSetsById(Long id)
    {
        return fxyFinancialAccountSetsMapper.selectFxyFinancialAccountSetsById(id);
    }

    /**
     * 查询账套管理列表
     * 
     * @param fxyFinancialAccountSets 账套管理
     * @return 账套管理
     */
    @Override
    public List<FxyFinancialAccountSets> selectFxyFinancialAccountSetsList(FxyFinancialAccountSets fxyFinancialAccountSets)
    {
        return fxyFinancialAccountSetsMapper.selectFxyFinancialAccountSetsList(fxyFinancialAccountSets);
    }

    /**
     * 新增账套管理
     * 
     * @param fxyFinancialAccountSets 账套管理
     * @return 结果
     */
    @Override
    public int insertFxyFinancialAccountSets(FxyFinancialAccountSets fxyFinancialAccountSets)
    {
        //创建账套
        int i = fxyFinancialAccountSetsMapper.insertFxyFinancialAccountSets(fxyFinancialAccountSets);
        //账套Id
        Long id = fxyFinancialAccountSets.getId();
        //查询出所有默认科目数据
        Map<Long, FxyFinancialSubject> objMap = new HashMap<>();

        List<FxyFinancialSubject> fxyFinancialSubjects = fxyFinancialSubjectMapper.selectFxyFinancialSubjectListDefault();
        for (FxyFinancialSubject fxyFinancialSubject : fxyFinancialSubjects) {
            //获取科目的原始ID
            Long beforeId = fxyFinancialSubject.getId();
            //取消掉数据库中原有的ID
            fxyFinancialSubject.setId(null);

            if(fxyFinancialSubject.getParentId() != 0L){
                FxyFinancialSubject newParent = objMap.get(fxyFinancialSubject.getParentId());
                fxyFinancialSubject.setParentId(newParent.getId());
            }

            fxyFinancialSubject.setAccountSetsId(id);

            //添加到数据库
            fxyFinancialSubjectMapper.insertFxyFinancialSubject(fxyFinancialSubject);
            objMap.put(beforeId, fxyFinancialSubject);
        }

        return i;
    }

    /**
     * 修改账套管理
     * 
     * @param fxyFinancialAccountSets 账套管理
     * @return 结果
     */
    @Override
    public int updateFxyFinancialAccountSets(FxyFinancialAccountSets fxyFinancialAccountSets)
    {
        return fxyFinancialAccountSetsMapper.updateFxyFinancialAccountSets(fxyFinancialAccountSets);
    }

    /**
     * 批量删除账套管理
     * 
     * @param ids 需要删除的账套管理主键
     * @return 结果
     */
    @Override
    public int deleteFxyFinancialAccountSetsByIds(Long[] ids)
    {
        return fxyFinancialAccountSetsMapper.deleteFxyFinancialAccountSetsByIds(ids);
    }

    /**
     * 删除账套管理信息
     * 
     * @param id 账套管理主键
     * @return 结果
     */
    @Override
    public int deleteFxyFinancialAccountSetsById(Long id)
    {
        return fxyFinancialAccountSetsMapper.deleteFxyFinancialAccountSetsById(id);
    }
}
